/*
 * @lc app=leetcode.cn id=475 lang=javascript
 *
 * [475] 供暖器
 */

// @lc code=start
/**
 * @param {number[]} houses
 * @param {number[]} heaters
 * @return {number}
 */
var binarySort = function(arr, x){
  let head = 0, tail = arr.length - 1, mid;
  while(head < tail){
    mid = (head + ((tail - head + 1) >> 1));
    if(arr[mid] <= x){
      head = mid;
    } else {
      tail = mid - 1;
    }
  }
  let res = arr[head + 1] ? Math.min( Math.abs(arr[head] - x), Math.abs(arr[head + 1] - x) ) : Math.abs((arr[head] - x));
  return res;
}
var findRadius = function(houses, heaters) {
  let arr = heaters.sort((a,b) => a - b);
  let max = 0;
  for (let i = 0; i < houses.length; i++) {
    let dis = binarySort(arr, houses[i]);
    // console.log(dis)
    if(dis > max) max = dis;
  }
  return max;
};
// @lc code=end

